import { http } from '@/utils/request'
import type { ApiResponse } from '@/types'

/**
 * 代码执行请求接口
 */
export interface ExecutionRequest {
  question_id: number
  code: string
  language: string
  input?: string
}

/**
 * 代码执行响应接口
 */
export interface ExecutionResponse {
  id: number
  status: 'success' | 'error' | 'timeout' | 'memory_limit'
  output: string
  error?: string
  time_used: number // 执行时间(毫秒)
  memory_used: number // 内存使用(KB)
  exit_code: number
  executed_at: string
}

/**
 * 代码提交请求接口
 */
export interface SubmissionRequest {
  question_id: number
  code: string
  language: string
}

/**
 * 代码提交响应接口
 */
export interface SubmissionResponse {
  id: number
  question_id: number
  status: 'accepted' | 'wrong_answer' | 'time_limit' | 'memory_limit' | 'runtime_error' | 'compile_error'
  score: number // 得分
  time_used: number // 执行时间(毫秒)
  memory_used: number // 内存使用(KB)
  test_cases: number // 测试用例总数
  passed_cases: number // 通过的测试用例数
  submitted_at: string
}

/**
 * 支持的语言接口
 */
export interface SupportedLanguage {
  value: string
  label: string
  ext: string
}

/**
 * 代码执行API
 */
export const executionApi = {
  // 运行代码
  runCode(data: ExecutionRequest): Promise<ApiResponse<ExecutionResponse>> {
    return http.post('/v1/execution/run', data)
  },

  // 提交代码进行评测
  submitCode(data: SubmissionRequest): Promise<ApiResponse<SubmissionResponse>> {
    return http.post('/v1/execution/submit', data)
  },

  // 获取支持的语言列表
  getSupportedLanguages(): Promise<ApiResponse<SupportedLanguage[]>> {
    return http.get('/v1/execution/languages')
  }
}
